;; Tests of the non-robust row echelon functions
;; Copyright (C) 2025  Rocks Mazama
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.linalg.r-e-f-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.linalg.r-e-f :as r-e-f]))

(defn- increasing?
  "Are the numbers in `coll` strictly increasing?"
  [coll]
  (or (empty? coll) (apply < coll)))

(defn- pivot-row?
  "Does `row` have a pivot in column `j`?"
  [j row]
  (and (->> row (take j) (every? zero?))
       (not (zero? (nth row j)))))

(defn- zero-rows?
  "Are the first `n` elements of every element of `rows` zero?"
  [n rows]
  (->> rows (map #(take n %)) (every? #(every? zero? %))))

(defn- r-e-f?
  [acols {:keys [pivot-column-indexes pivot-rows zero-rows]}]
  (and (increasing? pivot-column-indexes)
       (every? identity (map #(pivot-row? %1 %2) pivot-column-indexes pivot-rows))
       (zero-rows? acols zero-rows)))

(defn- check-r-e-f [{:keys [:ncols :acols] :as m}]
  (let [acols (or acols ncols)
        info (r-e-f/r-e-f m)]
    (is (r-e-f? acols info))))

(deftest not-augmented
  (testing "list"
    (check-r-e-f {:ncols 5 :data '(0 0 7 0 2
                                   2 0 0 0 3
                                   3 0 5 6 7
                                   3 0 5 8 1)}))
  )

(deftest augumented
  (testing "1"
    (check-r-e-f {:ncols 4 :acols 3 :data [1 2 3 3
                                           4 5 6 9
                                           7 8 9 16]}))
  (testing "3"
    (check-r-e-f {:ncols 6 :acol 3 :data [1 2 3 1 0 0
                                          4 5 6 0 1 0
                                          7 8 8 0 0 1]}))
  )
